package at.fpmedv.eclipse.helma.editappspropertiesbutton.actions;

import java.io.File;

import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.filesystem.IFileStore;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.ide.IDE;

import at.fpmedv.eclipse.helma.core.HelmaCorePlugin;
import at.fpmedv.eclipse.helma.preferences.HelmaConstants;

public class EditAppsPropertiesAction implements IWorkbenchWindowActionDelegate {
	
	/**
	 * name of the apps properties file
	 */
	static final String appsPropertiesFileName = "apps.properties";

	/**
	 * not needed
	 */
	public void dispose() {
	}

	/**
	 * not needed
	 */
	public void init(IWorkbenchWindow window) {
	}

	
	/**
	 * opens an editor in the current Workbench with the apps.properties
	 */
	public void run(IAction action) {
		IPreferenceStore store = HelmaCorePlugin.getDefault().getPreferenceStore();
		File fileToOpen = new File(store.getString(HelmaConstants.P_HELMA_HOME_PATH) + "/" + EditAppsPropertiesAction.appsPropertiesFileName);
		if (fileToOpen.exists() && fileToOpen.canRead()) {
			IFileStore fileStore = EFS.getLocalFileSystem().getStore(
					fileToOpen.toURI());
			IWorkbenchPage page = PlatformUI.getWorkbench()
					.getActiveWorkbenchWindow().getActivePage();
			try {
				IDE.openEditorOnFileStore(page, fileStore);
			} catch (PartInitException e) {
				// Put your exception handler here if you wish to
			}
		}
	}

	/**
	 * not needed
	 */
	public void selectionChanged(IAction action, ISelection selection) {
	}

}
